package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.person.common.BaseContext;
import com.person.mapper.ShoppingCartMapper;
import com.person.pojo.ShoppingCart;
import com.person.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service

public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    @Override

    public ShoppingCart inquire(Long dishId, Long setmealId) {
        ShoppingCart shoppingCart = shoppingCartMapper.inquire(dishId, setmealId,BaseContext.getCurrentId());

        return shoppingCart;
    }

    @Override
    public void add(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setId(IdUtil.getSnowflakeNextId());
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.add(shoppingCart);
    }

    @Override
    public void updateNumber(Long id) {
        shoppingCartMapper.updateNumber(id);

    }

    @Override
    public void sub(Long dishId, Long setmealId, Long userId) {
        shoppingCartMapper.update(dishId,setmealId,userId);
    }

    @Override
    public void delete(Long dishId, Long setmealId) {
        shoppingCartMapper.delete(dishId,setmealId,BaseContext.getCurrentId());
    }

    @Override
    public List<ShoppingCart> getlist(Long userId) {
        return shoppingCartMapper.list(userId);
    }

    @Override
    public void clean(Long userId) {
        shoppingCartMapper.deleteList(userId);
    }
}
